import os
import urllib2
import cookielib
import logging
import config as c


cj = cookielib.LWPCookieJar()
cookiefile = 'cookies.lwp'
header = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
Request = urllib2.Request
urlopen = urllib2.urlopen

if os.path.isfile(cookiefile):
    cj.load(cookiefile)

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)

def request_url(url, parser, data=None, header=header):
    log = logging.getLogger('TiVoToDo.request.request_url')
    if data is None:
        data = None
    try:
        req = Request(url, data, header)
        handle = urlopen(req)
        log.debug('We had to open %s ' % url)
    except IOError, e:
        log.error('We failed to open "%s".' % url)
        if hasattr(e, 'code'):
            log.error('We failed with error code - %s.' % e.code)
            exit()
    else:
        c.result = handle.read().replace('"big"', "'big'").replace('"ludacris"', "'ludacris'").replace("'+'", "")
        parser.feed(c.result)
        parser.close()
        parser.reset()